SET(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS ON)

# convient Macros
MACRO (EXCLUDE_SRC_FILE srcs_var file)
    GET_FILENAME_COMPONENT(abs_file_path ${file} ABSOLUTE)
    
    LIST(LENGTH ${srcs_var} srcs_count)
    
    SET(i 0)
    WHILE (i LESS ${srcs_count})
        LIST(GET ${srcs_var} ${i} src_file)
        GET_FILENAME_COMPONENT(abs_src_path ${src_file} ABSOLUTE)
        
        IF (abs_file_path STREQUAL ${abs_src_path})
            LIST(REMOVE_AT ${srcs_var} ${i}) # remove file from srcs list
            MATH(EXPR i "${i}+${srcs_count}") # break the loop
            MESSAGE("Excluded source file [${file}] from [${srcs_var}]")
        ENDIF()
        
        MATH(EXPR i "${i}+1")
    ENDWHILE()
ENDMACRO ()

MACRO (ADD_SRC_DIR srcs_var dir)    
    FILE(GLOB _mcaddsrc_src_ ${dir}/*.cpp ${dir}/*.c ${dir}/*.h)
    #SET(${var} ${_mcaddsrc_src_})
    #MESSAGE(${_mcaddsrc_src_})
    LIST(APPEND ${srcs_var} ${_mcaddsrc_src_})
    
    # remove ../ and ./
    STRING(REPLACE "../" "" group ${dir})
    STRING(REPLACE "./" "" group ${group})
    
    # change / to "\\"
    STRING(REPLACE / . group ${group})
    
    SOURCE_GROUP(${group} FILES ${_mcaddsrc_src_})
    
    MESSAGE("Added source directory [${dir}] to [${srcs_var}]")
ENDMACRO ()

MACRO (ADD_SRC_FILE srcs_var file)
    # MESSAGE(${dir})
    LIST(APPEND ${srcs_var} ${file})
    
    GET_FILENAME_COMPONENT(group ${file} PATH)
    # remove ../ and ./
    STRING(REPLACE "../" "" group ${group})
    STRING(REPLACE "./" "" group ${group})
    
    # change / to "\\"
    STRING(REPLACE / . group ${group})
    
    SOURCE_GROUP(${group} FILES ${file})
    MESSAGE("Added source file [${file}] to [${srcs_var}]")
ENDMACRO ()
